﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;



namespace IgnData
{
    public class IgnMap
    {
        private Bitmap bmp;
        private Bitmap layer2;
        private GeoRef georeference;

        internal IgnMap()
        {

        }

        public event EventHandler BmpChanged;

        public Bitmap Bmp
        {
            get
            {
                Bitmap result = new Bitmap(this.bmp);
                using (Graphics mapGrph = Graphics.FromImage(result))
                {
                    mapGrph.DrawImage(layer2, 0, 0);
                }
                return result;
            }
            set
            {
                this.bmp = value;
                this.layer2 = new Bitmap(value.Width, value.Height);
                using (Graphics mapGrph = Graphics.FromImage(this.layer2))
                {
                    mapGrph.Clear(Color.Transparent);
                }
            }
        }

        public GeoRef Georeference
        {
            get { return georeference; }
            set { georeference = value; }
        }

        public void Mark(GeoCoord utmPoint)
        {
            GeoCoord centralPxPoint = IgnConverter.UtmToPx(utmPoint);
            GeoCoord upperLeftPxPoint = IgnConverter.UtmToPx(new GeoCoord((int)this.Georeference.X, (int)this.Georeference.Y));
            double dX = centralPxPoint.X - upperLeftPxPoint.X;
            double dy = upperLeftPxPoint.Y - centralPxPoint.Y;
            Pen pen = new Pen(Color.DeepPink, 2);
            using (Graphics mapGrph = Graphics.FromImage(this.layer2))
            {
                mapGrph.DrawEllipse(pen, (float)dX - 4, (float)dy - 4, 8, 8);
                pen.Color = Color.Blue;
                mapGrph.DrawEllipse(pen, (float)dX - 8, (float)dy - 8, 16, 16);
            }
            ThrowBmpChanged();
        }

        public void DrawKmGrid()
        {
            int upperLeftUtmX = (int)this.Georeference.X;
            int upperLeftUtmY = (int)this.Georeference.Y;
            int scaleX = (int)this.Georeference.ScaleX;
            int scaleY = (int)this.Georeference.ScaleY;

            //desplazamiento
            int deltaX = 1000 - (upperLeftUtmX % 1000);
            int deltaY = (upperLeftUtmY % 1000) - 1000;
            int deltaI = deltaX / scaleX;
            int deltaJ = deltaY / scaleY;

            Pen pen = new Pen(Color.DarkMagenta, 1);
            Font drawFont = new Font("Arial", 12);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            using (Graphics mapGrph = Graphics.FromImage(this.layer2))
            {
                for (int gi = deltaI; gi < this.bmp.Width; gi += 1000 / scaleX)
                {
                    mapGrph.DrawLine(pen, gi, 0, gi, this.bmp.Width);
                    string pos = string.Format("x:{0}", (upperLeftUtmX + gi * 4).ToString("#,###"));
                    mapGrph.DrawString(pos, drawFont, drawBrush, gi, 0);
                }
                for (int gj = deltaJ; gj < this.bmp.Height; gj += 1000 / scaleY)
                {
                    mapGrph.DrawLine(pen, 0, gj, this.bmp.Height, gj);
                    string pos = string.Format("y:{0}", (upperLeftUtmY - gj * 4).ToString("#,###"));
                    mapGrph.DrawString(pos, drawFont, drawBrush, 0, gj);
                }
            }
            ThrowBmpChanged();
        }

        public void EraseKmGrid()
        {
            using (Graphics mapGrph = Graphics.FromImage(layer2))
            {
                mapGrph.Clear(Color.Transparent);
            }
            ThrowBmpChanged();
        }

        private void ThrowBmpChanged()
        {
            if (BmpChanged != null)
            {
                BmpChanged(this, new EventArgs());
            }
        }
    }
}
